#!/usr/bin/env python3
"""
创建测试共享文件数据
"""

import requests
import json
import os

# 配置
BASE_URL = "http://localhost:8000/api"
LOGIN_URL = f"{BASE_URL}/auth/login"
CREATE_FOLDER_URL = f"{BASE_URL}/files/folder"
UPLOAD_FILE_URL = f"{BASE_URL}/files/upload"

def login(username, password):
    """用户登录"""
    data = {
        "username": username,
        "password": password
    }
    
    response = requests.post(LOGIN_URL, json=data)
    if response.status_code == 200:
        result = response.json()
        print(f"✓ 用户 {username} 登录成功")
        return result["access_token"]
    else:
        print(f"✗ 用户 {username} 登录失败: {response.text}")
        return None

def create_folder(token, folder_name, parent_id=None, share_level="public"):
    """创建文件夹"""
    headers = {"Authorization": f"Bearer {token}"}
    data = {
        "name": folder_name,
        "parent_id": parent_id,
        "share_level": share_level
    }
    
    response = requests.post(CREATE_FOLDER_URL, json=data, headers=headers)
    if response.status_code == 200:
        result = response.json()
        print(f"✓ 创建文件夹 '{folder_name}' 成功 (共享级别: {share_level})")
        return result
    else:
        print(f"✗ 创建文件夹 '{folder_name}' 失败: {response.text}")
        return None

def upload_file(token, file_path, file_name, parent_id=None, share_level="public"):
    """上传文件"""
    headers = {"Authorization": f"Bearer {token}"}
    
    # 创建一个测试文件
    test_content = f"这是一个测试文件: {file_name}\n创建时间: {os.popen('date').read()}"
    
    files = {
        'file': (file_name, test_content, 'text/plain')
    }
    data = {
        'parent_id': parent_id,
        'share_level': share_level
    }
    
    response = requests.post(UPLOAD_FILE_URL, files=files, data=data, headers=headers)
    if response.status_code == 200:
        result = response.json()
        print(f"✓ 上传文件 '{file_name}' 成功 (共享级别: {share_level})")
        return result
    else:
        print(f"✗ 上传文件 '{file_name}' 失败: {response.text}")
        return None

def main():
    print("=== 创建测试共享文件数据 ===\n")
    
    # 管理员登录
    print("1. 管理员登录...")
    admin_token = login("admin", "admin123")
    if not admin_token:
        return
    
    # 创建公开共享文件夹
    print("\n2. 创建公开共享文件夹...")
    folder1 = create_folder(admin_token, "公开文档", None, "public")
    folder2 = create_folder(admin_token, "公开资源", None, "public")
    
    # 创建部门共享文件夹
    print("\n3. 创建部门共享文件夹...")
    folder3 = create_folder(admin_token, "技术部文档", None, "department")
    folder4 = create_folder(admin_token, "内部资料", None, "department")
    
    # 上传公开共享文件
    print("\n4. 上传公开共享文件...")
    if folder1:
        upload_file(admin_token, "", "用户手册.txt", folder1.get('id'), "public")
        upload_file(admin_token, "", "系统介绍.txt", folder1.get('id'), "public")
    if folder2:
        upload_file(admin_token, "", "下载资源.txt", folder2.get('id'), "public")
    
    # 上传部门共享文件
    print("\n5. 上传部门共享文件...")
    if folder3:
        upload_file(admin_token, "", "技术规范.txt", folder3.get('id'), "department")
        upload_file(admin_token, "", "开发指南.txt", folder3.get('id'), "department")
    if folder4:
        upload_file(admin_token, "", "内部通知.txt", folder4.get('id'), "department")
    
    # 管理员创建更多测试文件
    print("\n6. 创建更多测试文件...")
    folder5 = create_folder(admin_token, "用户分享", None, "public")
    if folder5:
        upload_file(admin_token, "", "分享文档.txt", folder5.get('id'), "public")
    upload_file(admin_token, "", "个人笔记.txt", None, "department")
    
    print("\n=== 测试数据创建完成 ===")
    print("\n现在可以运行 test_shared_files.py 来测试共享文件功能")

if __name__ == "__main__":
    main()